// 文档地址:
// 通用:https://github.com/jpush/jpush-hbuilder-plugin/blob/master/doc/API.md
// IOS:https://github.com/jpush/jpush-hbuilder-plugin/blob/master/doc/IOS.md
// Android:https://github.com/jpush/jpush-hbuilder-plugin/blob/master/doc/ANDROID.md
const jpushModule = uni.requireNativePlugin("JG-JPush")
// 离线推送启动应用后的跳转操作
export function startFromJpush() {
	console.log("执行123", uni.getStorageSync("jpush_info"))
	console.log(Boolean(uni.getStorageSync("jpush_info")))
	if (uni.getStorageSync("jpush_info")) {
		uni.reLaunch({
			url: '/pages/order_manage/order_manage',
			complete: (res) => {
				uni.removeStorageSync("jpush_info")
			}
		});
	}
}
// 获取到极光推送的消息通知后的操作
function addNotificationListener() {
	jpushModule.addNotificationListener((result) => {
		// 角标
		jpushModule.setBadge(0);
		plus.runtime.setBadgeNumber(0);
		console.log('推送的消息', result)
		uni.setStorageSync("jpush_info", result)
		console.log("存结果", uni.getStorageSync("jpush_info"))
		// 点击事件
		if (result.notificationEventType == 'notificationOpened') {
			if (uni.getSystemInfoSync().platform == 'ios') {
				uni.reLaunch({
					url: '/pages/order_manage/order_manage'
				});
			} else {
				uni.reLaunch({
					url: '/pages/order_manage/order_manage',
					fail: (err) => {
						console.log("跳转失败", err)
					},
					complete: (res) => {
						console.log("跳转完成", res)
					}
				});
			}
		}
	});
}
// 初始化推送服务
export function initJPushService() {
	addNotificationListener()
	jpushModule.initJPushService();
	checkNotificationStatus()
}
// 开启调试模式 params:true/false
export function setLoggerEnable(bool) {
	jpushModule.setLoggerEnable(bool);
}
// 检查当前通知是否是开启状态，未开启打开系统开启权限，已开启绑定设备到服务器
function checkNotificationStatus() {
	// ios和安卓有些api不一样，分别处理
	if (uni.getSystemInfoSync().platform == 'ios') {
		jpushModule.requestNotificationAuthorization((result) => {
			// 0 - NotDetermined 1 - Denied 2 - Authorized 3 - Provisional (ios 12 及以上才有)
			console.log('判断ios是否打开权限', result);
			if (result.status > 1) {
				// 已开启
				addConnectEventListener()
			} else {
				// uni.showModal({
				// 	title: '通知权限开启提醒',
				// 	content: '您还没有开启通知权限，无法接受到消息通知，请前往设置！',
				// 	showCancel: false,
				// 	confirmText: '去设置',
				// 	success: function(res) {
				// 		if (res.confirm) {
				// 			const app = plus.ios.invoke('UIApplication', 'sharedApplication');
				// 			const setting = plus.ios.invoke('NSURL', 'URLWithString:',
				// 				'app-settings:');
				// 			plus.ios.invoke(app, 'openURL:', setting);
				// 			plus.ios.deleteObject(setting);
				// 			plus.ios.deleteObject(app);
				// 		}
				// 	}
				// });
			}
		});
	} else {
		jpushModule.isNotificationEnabled((result) => {
			// -1 - 检测失败 0 - 关闭 1 - 开启
			console.log('判断android是否打开权限', result);
			if (result.code == 1) {
				// 已开启
				addConnectEventListener()
			} else {
				uni.showModal({
					title: '通知权限开启提醒',
					content: '您还没有开启通知权限，无法接受到消息通知，请前往设置！',
					showCancel: false,
					confirmText: '去设置',
					success: function(res) {
						//android打开对应的通知栏
						const main = plus.android.runtimeMainActivity();
						const pkName = main.getPackageName();
						const uid = main.getApplicationInfo().plusGetAttribute('uid');
						if (res.confirm) {
							const Intent = plus.android.importClass('android.content.Intent');
							const Build = plus.android.importClass('android.os.Build');
							//android 8.0引导
							if (Build.VERSION.SDK_INT >= 26) {
								const intent = new Intent(
									'android.settings.APP_NOTIFICATION_SETTINGS');
								intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);
							} else if (Build.VERSION.SDK_INT >= 21) {
								//android 5.0-7.0
								const intent = new Intent(
									'android.settings.APP_NOTIFICATION_SETTINGS');
								intent.putExtra('app_package', pkName);
								intent.putExtra('app_uid', uid);
							} else {
								//(<21)其他--跳转到该应用管理的详情页
								intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
								const uri = Uri.fromParts('package', mainActivity.getPackageName(),
									null);
								intent.setData(uri);
							}
							// 跳转到该应用的系统通知设置页
							main.startActivity(intent);
						}
					}
				});
			}
		});
	}
}
// 监听极光推送连接状态，并连接到后端服务器
export function addConnectEventListener() {
	jpushModule.addConnectEventListener(result => {
		console.log('监听极光推送连接状态', result);
	})
}